#include "message.h"

char Serdir[128] = "./serdir";
char Cliedir[128] = "./cliedir";
//处理输入的字符串对应的命令号,以及所需要的参数
enum CMD_NO get_cmd(char* send_buf,char* pathname)
{
    char *p = NULL;
    if(!strncmp(send_buf,"ls",strlen("ls")))
    {
        return FTP_CMD_LS;
    }
    else if(!strncmp(send_buf,"get",strlen("get")))
    {
        p = send_buf+strlen("get");
        while(*p == ' ')
        {
            p++;
        }
        while(*p!='\n')
        {
            *pathname++ = *p++;
        }
        return FTP_CMD_GET;
    }
    else if(!strncmp(send_buf,"put",strlen("put")))
    {
        p = send_buf+strlen("put");
        while(*p == ' ')
        {
            p++;
        }
        while(*p!='\n')
        {
            *pathname++ = *p++;
        }
        return FTP_CMD_PUT;
    }
    else if(!strncmp(send_buf,"cd",strlen("cd")))
    {
        p = send_buf+strlen("cd");
        while(*p == ' ')
        {
            p++;
        }
        while(*p!='\n')
        {
            *pathname++ = *p++;
        }
        return FTP_CMD_CD;
    }
    else if(!strncmp(send_buf,"pwd",strlen("pwd")))
    {
        return FTP_CMD_PWD;
    }
    else if(!strncmp(send_buf,"bye",strlen("bye")))
    {
        return FTP_CMD_BYE;
    }
    else
    {
        return FTP_CMD_UNKNOWN;
    }
}

void hand_cmd(unsigned char* recv_cmd,enum CMD_NO *cmd,int* arg_len,char* arg)//获取参数
{
    if(recv_cmd[0] != 0xC0)
    {
        printf("传输错误\n");
        return;
    }
    //int i = 0;
    //获取命令号
    *cmd = recv_cmd[5]|(recv_cmd[6]<<8)|(recv_cmd[7]<<16)|(recv_cmd[8]<<24);
    //printf("*cmd == %d\n",*cmd);
    if(*cmd == FTP_CMD_LS)
    {
        return;
    }
    else if(*cmd == FTP_CMD_GET)
    {
        //获取参数的长度
        *arg_len = recv_cmd[9]|(recv_cmd[10]<<8)|(recv_cmd[11]<<16)|(recv_cmd[12]<<24);
        //拷贝参数到参数数组当中
        strncpy(arg,recv_cmd+13,*arg_len);
        return;
    }
    else if(*cmd == FTP_CMD_PUT)
    {
        //获取参数的长度
        *arg_len = recv_cmd[9]|(recv_cmd[10]<<8)|(recv_cmd[11]<<16)|(recv_cmd[12]<<24);
        //拷贝参数到参数数组当中
        strncpy(arg,recv_cmd+13,*arg_len);
        return;
    }
    else if(*cmd == FTP_CMD_CD)
    {
        //获取参数的长度
        *arg_len = recv_cmd[9]|(recv_cmd[10]<<8)|(recv_cmd[11]<<16)|(recv_cmd[12]<<24);
        //拷贝参数到参数数组当中
        strncpy(arg,recv_cmd+13,*arg_len);
        return;
    }
    else if(*cmd == FTP_CMD_BYE)
    {
        return;
    }
    else if(*cmd==FTP_CMD_PWD)
    {
        return;
    }
    else if(*cmd == FTP_CMD_UNKNOWN)
    {

        return;
    }
}
